﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.Net;
using AfvandingService.DOAs;
using System.Threading;

namespace AfvandingService
{
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class Central : ICentral
    {
        #region Fields
        private List<Pumpestation> _pumpestationsliste;
        private ICentralDOA _dao;
        private Random random = new Random();
        #endregion

        #region ctor
        public Central()
        {
            _dao = new CentralDOA();
            _pumpestationsliste = _dao.HentPumpestationerOgPumper();
            Thread traad = new Thread(Nedbør);
            traad.Start();
        } 
        #endregion

        #region Methods

            #region Pumpestation

            public void OpretPumpestation(int pumpestationsnr, string netværksaddresse, string IP, int port, string vandstandsrisiko, double vandstand)
            {
                Pumpestation pust = new Pumpestation(pumpestationsnr, netværksaddresse, IP, port, vandstandsrisiko, vandstand);
                _pumpestationsliste.Add(pust);
                _dao.OpretPumpestation(pust);
            }

            public void OpdatérPumpestation(int pumpestationsnr, string netværksaddresse, string IP, int port, string vandstandsrisiko, double vandstand)
            {
                foreach (var station in _pumpestationsliste)
                {
                    if (station.Pumpestationsnr == pumpestationsnr)
                    {
                        station.Netværksadresse = netværksaddresse;
                        station.IP = IP;
                        station.Port = port;
                        station.Vandstandsrisiko = vandstandsrisiko;
                        station.Vandstand = vandstand;
                        _dao.OpdatérPumpestation(pumpestationsnr, netværksaddresse, IP, port, vandstandsrisiko, vandstand);
                    }
                }
            }

            public void SletPumpestation(int pumpestationsnr)
            {
                foreach (var item in _pumpestationsliste)
                {
                    if (item.Pumpestationsnr == pumpestationsnr)
                    {
                        _pumpestationsliste.Remove(item);
                        _dao.SletPumpestation(pumpestationsnr);
                        return;
                    }
                }
            }

            public List<Pumpestation> PumpestationsListe() 
            {
                return _pumpestationsliste;
            }

            public void Nedbør()
            {
                while (true)
                {
                    int nedbør = random.Next(0, 10);

                    foreach (var pumpestation in _pumpestationsliste)
                    {
                        pumpestation.Vandstand += nedbør;
                    }
                    Thread.Sleep(10000); 
                }
            }

            #endregion

            #region Pumpe

            public void AnmodOpretPumpe(int pumpestationsnr, double pumpekapacitet, int pumpenr)
            {
                {
                    foreach (var item in _pumpestationsliste)
                    {
                        if (item.Pumpestationsnr == pumpestationsnr)
                        {
                            item.OpretPumpe(pumpekapacitet, pumpenr, pumpestationsnr);
                        }
                    }
                }
            }

            public void AnmodOpdatérPumpe(int psnr, double pumpekapacitet, int pumpenr)
            {
                foreach (var pumpestation in PumpestationsListe())
                {
                    pumpestation.OpdatérPumpe(psnr, pumpekapacitet, pumpenr);
                }
            }

            public void AnmodSletPumpe(int pumpestationsnr, int pumpenr)
            {
                foreach (var pumpestation in PumpestationsListe())
                {
                    if (pumpestation.Pumpestationsnr == pumpestationsnr)
                    {
                        pumpestation.SletPumpe(pumpestationsnr, pumpenr);
                        break;
                    }
                }
            }

            public bool AnmodStart(int pumpenr, int pumpestationsnr)
            {
                foreach (var pumpestation in _pumpestationsliste)
                {
                    if (pumpestation.Pumpestationsnr == pumpestationsnr)
                    {
                        pumpestation.AnmodStartPumpe(pumpenr, pumpestation);
                        pumpestation.DAO.ÆndrPumpeStatus(pumpestationsnr, pumpenr, true);
                        return true;
                    }
                }
                return false;
            }

            public bool AnmodStop(int pumpenr, int pumpestationsnr)
            {
                foreach (var pumpestation in _pumpestationsliste)
                {
                    if (pumpestation.Pumpestationsnr == pumpestationsnr)
                    {
                        pumpestation.AnmodStopPumpe(pumpenr, pumpestationsnr);
                        pumpestation.DAO.ÆndrPumpeStatus(pumpestationsnr, pumpenr, false);
                        return true;
                    }
                }
                return false;
            }

            #endregion

            #region Vandstand

            public double EnPSVandstand(int pumpestationsnr)
            {
                // return PumpestationVandstand(pumpestation).ToString();

                return _dao.EnPSVandstand(pumpestationsnr);
            }

            public List<double> AllePSVandstande()
            {
                //List<string> vandstandsListe = new List<string>();
                //foreach (var pumpestation in _pumpestationsliste)
                //{
                //    vandstandsListe.Add(pumpestation.AnmodningVandstand());
                //}
                // return vandstandsListe;
                 
                return _dao.AllePSVandstande();
            }

            public Dictionary<double, DateTime> HentLog(int pumpestationsNr)
            {
                return _dao.HentLog(pumpestationsNr);
            }

            #endregion

        #endregion
    }
}